package com.cloudkinto.controller.freemakertemplate;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import com.cloudkinto.entity.FreeMarkerTemplateRulesDo;
import com.cloudkinto.service.freemarkdertemplate.FreeMarkerTemplateService;
import com.cloudkinto.service.freemarkdertemplate.vo.FreeMarkerTemplateDeleteRulesReq;
import com.cloudkinto.service.freemarkdertemplate.vo.FreeMarkerTemplateReq;
import com.cloudkinto.service.freemarkdertemplate.vo.FreeMarkerTemplateRulesReq;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "freeMarker模板在线编辑")
@RequestMapping("/api/admin/oms/template")
public class FreeMarkerTemplateController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(FreeMarkerTemplateController.class);

 @Autowired
    private FreeMarkerTemplateService service;


    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "name", value = "模板名称"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
             })
    public PageResult listPage(HttpServletRequest request){
        Long companyId = getCompanyId();
        Long userId = getCurrentUserId();

        Map<String, String> map = new HashMap<>();
        String name = request.getParameter("name");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String startDate = request.getParameter("startDate");
        map.put("current", current);
        map.put("pageSize", size);
        map.put("name",name);
        map.put("startDate", startDate);
        if(companyId != null){
            map.put("companyId", companyId+"");
        }

        return service.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "freeMarker模板详情", notes = "根据id查看详情")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "模板id")})
    public SingleResult freeMarkerTemplateDetail(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("")
    @ApiOperation(value = "freeMarker模板规则列表", notes = "freeMarker模板规则列表")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "模板id")})
    public SingleResult freeMarkerTemplateRulesList(Long id) {
        return SingleResult.success( service.rulesList(id));
    }

    @UserSessionRequired
    @GetMapping("/placeholder")
    @ApiOperation(value = "freeMarker占位符列表", notes = "freeMarker占位符列表")
 /*   @ApiImplicitParams(
            {@ApiImplicitParam(name = "type", value = "类型id {1:基本信息 2：配送信息 3:产品列表  }")})*/
    public SingleResult freeMarkerTemplatePlaceholderList() {
        return SingleResult.success( service.placeHolderList());
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "id", value = "模板id")})

    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功",service.delete(id));
    }

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "保存", notes = "保存")
    public SingleResult add(@RequestBody @Validated FreeMarkerTemplateReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        service.add(req,userId);
        return SingleResult.success("保存成功");
    }

/*    @UserSessionRequired
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated FreeMarkerTemplateRulesDo req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功");
    }*/

/*    @UserSessionRequired
    @PostMapping("/addRules")
    @ApiOperation(value = "添加占位符", notes = "添加占位符")
    public SingleResult add(@RequestBody @Validated FreeMarkerTemplateRulesReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        Map<String, Object> stringObjectMap = service.addRules(req, userId);
        if(stringObjectMap.size()>0){
            return SingleResult.failure("新增占位符失败"+"["+stringObjectMap.get("ruleName")+"]"+"已经存在！");
        }
        return SingleResult.success("新增占位符成功");
    }*/
    /*@UserSessionRequired
    @DeleteMapping("/deleteRules")
    @ApiOperation(value = "删除占位符", notes = "删除占位符")
    public SingleResult add(@RequestBody @Validated FreeMarkerTemplateDeleteRulesReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        service.deleteRule(req);
        return SingleResult.success("删除占位符成功");
    }*/

}
